home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / components / calItemModule.js < prev    next >
Text File  |  2008-02-25  |  13KB  |  329 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Oracle Corporation code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  *  Oracle Corporation
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
  24.  *   Mike Shaver <shaver@off.net>
  25.  *   Matthew Willis <lilmatt@mozilla.com>
  26.  *   Philipp Kewisch <mozilla@kewis.ch>
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the MPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the MPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41.  
  42. const kCalRecurrenceInfoContractID = "@mozilla.org/calendar/recurrence-info;1";
  43. const kCalIRecurrenceInfo = Components.interfaces.calIRecurrenceInfo;
  44.  
  45. const kCalRecurrenceRuleContractID = "@mozilla.org/calendar/recurrence-rule;1";
  46. const kCalIRecurrenceRule = Components.interfaces.calIRecurrenceRule;
  47.  
  48. const kCalRecurrenceDateSetContractID = "@mozilla.org/calendar/recurrence-date-set;1";
  49. const kCalIRecurrenceDateSet = Components.interfaces.calIRecurrenceDateSet;
  50.  
  51. const kCalRecurrenceDateContractID = "@mozilla.org/calendar/recurrence-date;1";
  52. const kCalIRecurrenceDate = Components.interfaces.calIRecurrenceDate;
  53.  
  54. /* Constructor helpers, lazily initialized to avoid registration races. */
  55. var CalRecurrenceInfo = null;
  56. var CalRecurrenceRule = null;
  57. var CalRecurrenceDateSet = null;
  58. var CalRecurrenceDate = null;
  59. var CalDateTime = null;
  60. var CalDuration = null;
  61. var CalAttendee = null;
  62. var CalItipItem = null;
  63.  
  64. var componentInitRun = false;
  65. function initBaseComponent()
  66. {
  67.     CalRecurrenceInfo = new Components.Constructor(kCalRecurrenceInfoContractID, kCalIRecurrenceInfo);
  68.     CalRecurrenceRule = new Components.Constructor(kCalRecurrenceRuleContractID, kCalIRecurrenceRule);
  69.     CalRecurrenceDateSet = new Components.Constructor(kCalRecurrenceDateSetContractID, kCalIRecurrenceDateSet);
  70.     CalRecurrenceDate = new Components.Constructor(kCalRecurrenceDateContractID, kCalIRecurrenceDate);
  71.  
  72.     CalDateTime = new Components.Constructor("@mozilla.org/calendar/datetime;1",
  73.                                              Components.interfaces.calIDateTime);
  74.     CalDuration = new Components.Constructor("@mozilla.org/calendar/duration;1",
  75.                                              Components.interfaces.calIDateTime);
  76.     CalAttendee = new Components.Constructor("@mozilla.org/calendar/attendee;1",
  77.                                              Components.interfaces.calIAttendee);
  78.     CalItipItem = new Components.Constructor("@mozilla.org/calendar/itip-item;1",
  79.                                              Components.interfaces.calIItipItem);
  80. }
  81.  
  82.  
  83. /* Update these in calBaseCID.h */
  84. const componentData =
  85.     [
  86.      /* calItemBase and calUtils must be first: later scripts depend on them */
  87.     {cid: null,
  88.      contractid: null,
  89.      script: "calItemBase.js",
  90.      constructor: null},
  91.  
  92.     {cid: null,
  93.      contractid: null,
  94.      script: "calUtils.js",
  95.      constructor: null},
  96.  
  97.     {cid: null,
  98.      contractid: null,
  99.      script: "calCachedCalendar.js",
  100.      constructor: null},
  101.  
  102.     {cid: Components.ID("{f42585e7-e736-4600-985d-9624c1c51992}"),
  103.      contractid: "@mozilla.org/calendar/manager;1",
  104.      script: "calCalendarManager.js",
  105.      constructor: "calCalendarManager",
  106.      categories: {"app-startup": "calendar-mananger-startup"},
  107.      service: true,
  108.      onComponentLoad: "onCalCalendarManagerLoad()"},
  109.  
  110.     {cid: Components.ID("{7a9200dd-6a64-4fff-a798-c5802186e2cc}"),
  111.      contractid: "@mozilla.org/calendar/alarm-service;1",
  112.      script: "calAlarmService.js",
  113.      constructor: "calAlarmService",
  114.      categories: {"app-startup": "alarm-service-startup"},
  115.      service: true},
  116.  
  117.     {cid: Components.ID("{29C56CD5-D36E-453a-ACDE-0083BD4FE6D3}"),
  118.      contractid: "@mozilla.org/calendar/freebusy-service;1",
  119.      script: "calFreeBusyService.js",
  120.      constructor: "calFreeBusyService",
  121.      service: true},
  122.  
  123.     {cid: Components.ID("{F5F743CD-8997-428e-BC1B-644E73F61203}"),
  124.      contractid: "@mozilla.org/calendar/calendarsearch-service;1",
  125.      script: "calCalendarSearchService.js",
  126.      constructor: "calCalendarSearchService",
  127.      service: true},
  128.  
  129.     {cid: Components.ID("{4b7ae030-ed79-11d9-8cd6-0800200c9a66}"),
  130.      contractid: "@mozilla.org/calendar/alarm-monitor;1",
  131.      script: "calAlarmMonitor.js",
  132.      constructor: "calAlarmMonitor",
  133.      service: true,
  134.      categories: {"alarm-service-startup": "calendar-alarm-monitor",
  135.                   "alarm-service-shutdown": "calendar-alarm-monitor"}},
  136.  
  137.     {cid: Components.ID("{974339d5-ab86-4491-aaaf-2b2ca177c12b}"),
  138.      contractid: "@mozilla.org/calendar/event;1",
  139.      script: "calEvent.js",
  140.      constructor: "calEvent"},
  141.  
  142.     {cid: Components.ID("{7af51168-6abe-4a31-984d-6f8a3989212d}"),
  143.      contractid: "@mozilla.org/calendar/todo;1",
  144.      script: "calTodo.js",
  145.      constructor: "calTodo"},
  146.  
  147.     {cid: Components.ID("{5c8dcaa3-170c-4a73-8142-d531156f664d}"),
  148.      contractid: "@mozilla.org/calendar/attendee;1",
  149.      script: "calAttendee.js",
  150.      constructor: "calAttendee"},
  151.  
  152.     {cid: Components.ID("{5f76b352-ab75-4c2b-82c9-9206dbbf8571}"),
  153.      contractid: "@mozilla.org/calendar/attachment;1",
  154.      script: "calAttachment.js",
  155.      constructor: "calAttachment"},
  156.  
  157.     {cid: Components.ID("{04027036-5884-4a30-b4af-f2cad79f6edf}"),
  158.      contractid: "@mozilla.org/calendar/recurrence-info;1",
  159.      script: "calRecurrenceInfo.js",
  160.      constructor: "calRecurrenceInfo"},
  161.  
  162.     {cid: Components.ID("{4123da9a-f047-42da-a7d0-cc4175b9f36a}"),
  163.      contractid: "@mozilla.org/calendar/datetime-formatter;1",
  164.      script: "calDateTimeFormatter.js",
  165.      constructor: "calDateTimeFormatter"},
  166.  
  167.     {cid: Components.ID("{6877bbdd-f336-46f5-98ce-fe86d0285cc1}"),
  168.      contractid: "@mozilla.org/calendar/weektitle-service;1",
  169.      script: "calWeekTitleService.js",
  170.      constructor: "calWeekTitleService"},
  171.  
  172.     {cid: Components.ID("{f41392ab-dcad-4bad-818f-b3d1631c4d93}"),
  173.      contractid: "@mozilla.org/calendar/itip-item;1",
  174.      script: "calItipItem.js",
  175.      constructor: "calItipItem"},
  176.  
  177.     {cid: Components.ID("{9787876b-0780-4464-8282-b7f86fb221e8}"),
  178.      contractid: "@mozilla.org/calendar/itip-processor;1",
  179.      script: "calItipProcessor.js",
  180.      constructor: "calItipProcessor"},
  181.  
  182.     {cid: Components.ID("{1e2fc0e2-bf5f-4d60-9f1e-5e92cf517c0b}"),
  183.      contractid: "@mozilla.org/network/protocol;1?name=webcal",
  184.      script: "calProtocolHandler.js",
  185.      constructor: "calProtocolHandler"},
  186.  
  187.     {cid: Components.ID("{6fe88047-75b6-4874-80e8-5f5800f14984}"),
  188.      contractid: "@mozilla.org/calendar/ics-parser;1",
  189.      script: "calIcsParser.js",
  190.      constructor: "calIcsParser"},
  191.  
  192.     {cid: Components.ID("{207a6682-8ff1-4203-9160-729ec28c8766}"),
  193.      contractid: "@mozilla.org/calendar/ics-serializer;1",
  194.      script: "calIcsSerializer.js",
  195.      constructor: "calIcsSerializer"},
  196.  
  197.     {cid: Components.ID("{40a1ccf4-5f54-4815-b842-abf06f84dbfd}"),
  198.      contractid: "@mozilla.org/calendar/transactionmanager;1",
  199.      script: "calTransactionManager.js",
  200.      constructor: "calTransactionManager"}
  201.     ];
  202.  
  203. var calItemModule = {
  204.     mScriptsLoaded: false,
  205.     loadScripts: function () {
  206.         if (this.mScriptsLoaded)
  207.             return;
  208.  
  209.         const jssslContractID = "@mozilla.org/moz/jssubscript-loader;1";
  210.         const jssslIID = Components.interfaces.mozIJSSubScriptLoader;
  211.  
  212.         const dirsvcContractID = "@mozilla.org/file/directory_service;1";
  213.         const propsIID = Components.interfaces.nsIProperties;
  214.  
  215.         const iosvcContractID = "@mozilla.org/network/io-service;1";
  216.         const iosvcIID = Components.interfaces.nsIIOService;
  217.  
  218.         var loader = Components.classes[jssslContractID].getService(jssslIID);
  219.         var dirsvc = Components.classes[dirsvcContractID].getService(propsIID);
  220.         var iosvc = Components.classes[iosvcContractID].getService(iosvcIID);
  221.  
  222.         // Note that unintuitively, __LOCATION__.parent == .
  223.         // We expect to find the subscripts in ./../js
  224.         var appdir = __LOCATION__.parent.parent;
  225.         appdir.append("js");
  226.  
  227.         for (var i = 0; i < componentData.length; i++) {
  228.             var scriptName = componentData[i].script;
  229.             if (!scriptName)
  230.                 continue;
  231.  
  232.             var f = appdir.clone();
  233.             f.append(scriptName);
  234.  
  235.             try {
  236.                 var fileurl = iosvc.newFileURI(f);
  237.                 loader.loadSubScript(fileurl.spec, null);
  238.             } catch (e) {
  239.                 dump("Error while loading " + fileurl.spec + "\n");
  240.                 throw e;
  241.             }
  242.         }
  243.  
  244.         this.mScriptsLoaded = true;
  245.     },
  246.  
  247.     registerSelf: function (compMgr, fileSpec, location, type) {
  248.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  249.  
  250.         var catman = Components.classes["@mozilla.org/categorymanager;1"]
  251.             .getService(Components.interfaces.nsICategoryManager);
  252.         for (var i = 0; i < componentData.length; i++) {
  253.             var comp = componentData[i];
  254.             if (!comp.cid)
  255.                 continue;
  256.             compMgr.registerFactoryLocation(comp.cid,
  257.                                             "",
  258.                                             comp.contractid,
  259.                                             fileSpec,
  260.                                             location,
  261.                                             type);
  262.  
  263.             if (comp.categories) {
  264.                 var contractid;
  265.                 if (comp.service)
  266.                     contractid = "service," + comp.contractid;
  267.                 else
  268.                     contractid = comp.contractid;
  269.                 for (var category in comp.categories) {
  270.                     catman.addCategoryEntry(category, comp.categories[category],
  271.                                             contractid, true, true);
  272.                 }
  273.             }
  274.         }
  275.     },
  276.  
  277.     makeFactoryFor: function(constructor) {
  278.         var factory = {
  279.             QueryInterface: function (aIID) {
  280.                 if (!aIID.equals(Components.interfaces.nsISupports) &&
  281.                     !aIID.equals(Components.interfaces.nsIFactory))
  282.                     throw Components.results.NS_ERROR_NO_INTERFACE;
  283.                 return this;
  284.             },
  285.  
  286.             createInstance: function (outer, iid) {
  287.                 if (outer != null)
  288.                     throw Components.results.NS_ERROR_NO_AGGREGATION;
  289.                 return (new constructor()).QueryInterface(iid);
  290.             }
  291.         };
  292.  
  293.         return factory;
  294.     },
  295.  
  296.     getClassObject: function (compMgr, cid, iid) {
  297.         if (!iid.equals(Components.interfaces.nsIFactory))
  298.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  299.  
  300.         if (!this.mScriptsLoaded)
  301.             this.loadScripts();
  302.  
  303.         if (!componentInitRun) {
  304.             initBaseComponent();
  305.             componentInitRun = true;
  306.         }
  307.  
  308.         for (var i = 0; i < componentData.length; i++) {
  309.             if (cid.equals(componentData[i].cid)) {
  310.                 if (componentData[i].onComponentLoad) {
  311.                     eval(componentData[i].onComponentLoad);
  312.                 }
  313.                 // eval to get usual scope-walking
  314.                 return this.makeFactoryFor(eval(componentData[i].constructor));
  315.             }
  316.         }
  317.  
  318.         throw Components.results.NS_ERROR_NO_INTERFACE;
  319.     },
  320.  
  321.     canUnload: function(compMgr) {
  322.         return true;
  323.     }
  324. };
  325.  
  326. function NSGetModule(compMgr, fileSpec) {
  327.     return calItemModule;
  328. }
  329.